package com.smarthome.api.mobile;

import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.smarthome.core.payment.alipay.AlipayService;
import com.smarthome.core.payment.alipay.util.BankRes;
import com.smarthome.domain.mobile.MobileConstants;
import com.smarthome.domain.mobile.MobileResponse;

/**
 * 手机APP的接口网关入口。
 *
 *
 * @author KongXiangyu
 *
 * @since 1.0
 */
@Controller
@RequestMapping("/mobile")
public class MobileGateway {

    private static final Logger LOGGER = LoggerFactory.getLogger(MobileGateway.class);
    @Resource
    private AlipayService alipayService;
    @Resource
    private ServiceExporter mobileServiceExporter;

    @RequestMapping(value = "/gateway.do", produces = { "text/json" }, method = { RequestMethod.GET, RequestMethod.POST })
    public void service(HttpServletRequest req, HttpServletResponse res) throws Exception {
        String reqStr = "";
        Map<String, String> params = new HashMap<String, String>();
        Enumeration<String> parameterNames = req.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String name = parameterNames.nextElement();
            params.put(name, req.getParameter(name));
            reqStr += name + "=" + req.getParameter(name) + "&";
        }
        LOGGER.info("服务端收到手机端发送的消息:" + reqStr);
        if (null != params && params.size() > 0) {
            String serviceName = params.get(MobileConstants.MOBILE_SERVICE_NAME);
            if (!StringUtils.isBlank(serviceName)) {
                MobileResponse<?> response = mobileServiceExporter.doService(params);
                String responseStr = mobileServiceExporter.parseJsonString(response, null);
                LOGGER.info("mobile-gateway-responseStr:" + responseStr);
                mobileServiceExporter.sendResponse(responseStr, res);
            }
        }
    }

    @RequestMapping(value = "/notify", produces = { "text/json" }, method = { RequestMethod.GET, RequestMethod.POST })
    public void notify(HttpServletRequest req, HttpServletResponse res, PrintWriter printWriter) throws Exception {
        String reqStr = "";
        Map<String, String> params = new HashMap<String, String>();
        Enumeration<String> parameterNames = req.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String name = parameterNames.nextElement();
            params.put(name, req.getParameter(name));
            reqStr += name + "=" + req.getParameter(name) + "&";
        }
        LOGGER.info("收到通知消息:" + reqStr);
        BankRes bankRes = new BankRes();
        bankRes.setNotifyData(reqStr);
        bankRes.setParams(params);
        alipayService.doPayByAlipay(bankRes);
        printWriter.write("success");
        printWriter.flush();
        printWriter.close();
    }
}
